//
//  ZPMineScoreViewController.m
//  ZPIOSEducation
//
//  Created by 李伟 on 2016/12/3.
//  Copyright © 2016年 mini1. All rights reserved.
//

#import "ZPMineScoreViewController.h"
#import "ZPScoreRuleViewController.h"
#import "ZPScoreOneTabViewCell.h"
#import "ZPScoreTwoTabViewCell.h"
#import "MJRefresh.h"

#define zFirstcellH 88
#define zSecondCellH 54

@interface ZPMineScoreViewController ()
{

    NSMutableArray  *scoreArray;
}

@property (strong, nonatomic) IBOutlet UITableView *scoreTableView;
@end

static NSString *const oneReuseIdentifier = @"ZPScoreOneTabViewCell";
static NSString *const twoReuseIdentifier = @"ZPScoreTwoTabViewCell";

@implementation ZPMineScoreViewController



- (void)requetScoreData
{
        [self  showHudithString:@"加载中..."];
        NSDictionary  *paramDic = [NSDictionary  dictionaryWithObjectsAndKeys:[[ZPAuthStore  sharedZPAuthStore].userToDic  objectForKey:@"memberId"] ,@"memberId", nil];
        [[ZPNetworking  sharedZPNetworking] POST:APIUC@"user/getScoreDetail.json" parameters:paramDic progress:^(NSProgress * _Nullable progress) {
            
        } success:^(id  _Nonnull responseObject) {
            [self  hiddenHud];
            if(CodeIs200(responseObject)){
                NSMutableArray *tempArray = [[responseObject objectForKey:@"result"]objectForKey:@"scoreDetail"];
                [scoreArray removeAllObjects];
                [scoreArray addObjectsFromArray:tempArray];
                if (self.scoreTableView.header.isRefreshing) {
                    [self.scoreTableView.header  endRefreshing];
                }
            }
             [self.scoreTableView reloadData];
            
        } failure:^(NSError * _Nonnull error) {
            [self  hiddenHud];
            if (self.scoreTableView.header.isRefreshing) {
                [self.scoreTableView.header  endRefreshing];
            }
            
        }];
}

#pragma mark - <viewDidLoad>
- (void)viewDidLoad {
    [super viewDidLoad];
    scoreArray = [[NSMutableArray  alloc] init];
    [self setupNavigationItem];
    [self setupViewWithNib];
    [self setupTableView];
    [self requetScoreData];
}

#pragma mark - <viewWillAppear>
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
}

- (void)viewDidDisappear:(BOOL)animated {
    [self.tabBarController.tabBar setHidden:NO];
}

#pragma mark - <setupNavigationItem>
- (void)setupNavigationItem {
    self.navigationItem.title = @"我的积分";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zhenpin_back"] style:UIBarButtonItemStylePlain target:self action:@selector(clickBackButtonItem)];
}
- (void)clickBackButtonItem {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - <setupViewWithNib>
- (void)setupViewWithNib {
    self.view = [[[NSBundle mainBundle] loadNibNamed:@"ZPMineScoreViewController" owner:self options:nil] lastObject];
}

#pragma mark - <setupTableView>
- (void)setupTableView {
    [self.scoreTableView registerNib:[UINib nibWithNibName:NSStringFromClass([ZPScoreOneTabViewCell class]) bundle:nil] forCellReuseIdentifier:oneReuseIdentifier];
    [self.scoreTableView registerNib:[UINib nibWithNibName:NSStringFromClass([ZPScoreTwoTabViewCell class]) bundle:nil] forCellReuseIdentifier:twoReuseIdentifier];
    self.scoreTableView.delegate =   self;
    self.scoreTableView.dataSource = self;
    
    __weak ZPMineScoreViewController *weakSelf = self;
    [self.scoreTableView  addLegendHeaderWithRefreshingBlock:^{
        [weakSelf  requetScoreData];
    }];
}

#pragma mark - <UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    } else {
        return scoreArray.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        ZPScoreOneTabViewCell *oneCell = [tableView dequeueReusableCellWithIdentifier:oneReuseIdentifier];
        if (oneCell == nil)
        {
            oneCell = [[ZPScoreOneTabViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:oneReuseIdentifier];
        }
        [oneCell.ruleScore addTarget:self action:@selector(ruleScoreClick) forControlEvents:UIControlEventTouchUpInside];
        oneCell.cumulativeScore.text = _score;
        return oneCell;
    }
    else if (indexPath.section == 1)
    {
        ZPScoreTwoTabViewCell *twoCell = [tableView dequeueReusableCellWithIdentifier:twoReuseIdentifier];
        if (twoCell == nil)
        {
            twoCell = [[ZPScoreTwoTabViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:twoReuseIdentifier];
        }
        NSDictionary *sectionDic = [scoreArray  objectAtIndex:indexPath.row];
        twoCell.headerTitle.text = @"签到";
        
        twoCell.detailedTitle.text = [NSString stringWithFormat:@"+%@",[sectionDic objectForKey:@"score"]];

        twoCell.dateTitle.text = [sectionDic objectForKey:@"createTimeString"];
        return twoCell;
    }
    return nil;
}

#pragma mark - <UITableViewDelegate>
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return zTableViewGroupSpacing_8;
    }
    return zTableViewGroupSpacing_0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return zTableViewGroupSpacing_0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return zFirstcellH;
    } else if (indexPath.section == 1) {
        return zSecondCellH;
    } return 0;
}


#pragma mark - <点击积分规则>
- (void)ruleScoreClick
{
    [self.navigationController pushViewController:[[ZPScoreRuleViewController alloc] init] animated:YES];
}
#pragma mark - <didReceiveMemoryWarning>
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
